//
//  itootSound.m
//  itootTest
//
//  Created by DAVID TESSITORE on 1/25/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "itootSound.h"

@implementation itootSound 


-(id) initWithFileName:(NSString *)sndFileName
{

	[super init];
	sndFileToPlay = [[NSString alloc] initWithString:sndFileName];
	[sndFileToPlay retain];
	return self;

}




-(void)dealloc {
    AudioServicesDisposeSystemSoundID(_soundID);
	[sndFileToPlay release];
	
    [super dealloc];
}


-(void)vibrate
{
	AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
}


-(void)play
{
	[self playSound:sndFileToPlay];
	
}



-(void)playSound:(NSString *)fileName
{
	NSString *fname, *ext;
	
	NSRange range = [fileName rangeOfString:@"."];
	int location = range.location;
	if( location > 0 )
	{
		fname = [fileName substringWithRange:NSMakeRange(0, location)];
		ext = [fileName substringFromIndex:location+1];
		[self playSound:fname :ext];
	}
	
	
	
}



-(void)playSound:(NSString *)fileName:(NSString *)fileExt

{

    NSBundle *mainBundle = [NSBundle mainBundle];
	
	NSURL *aFileURL = [NSURL fileURLWithPath:[mainBundle pathForResource:fileName ofType:fileExt] isDirectory:NO];
	
	if (aFileURL != nil) 
	{
		SystemSoundID aSoundID;
		OSStatus error = AudioServicesCreateSystemSoundID((CFURLRef)aFileURL, &aSoundID);
		
		if (error == kAudioServicesNoError)
		{ // success
			_soundID = aSoundID;
			 AudioServicesPlaySystemSound(_soundID);
			
		}
		else
		{
			NSLog(@"Error %d loading sound at path: %@", error, fileName);
		}
    	
	}
}


@end

